<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2015 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: Newsword.php 60982 2018-09-04 09:28:55 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：对应的表名:tky_newsword
 * +----------------------------------------------------------------------
 */

namespace application\admin\controller;

use ticky\request;
use ticky\response;

class newsword extends base {

    //首页
    public function index() {
        $search = $this->search_frm();
        $ret = db('newsword')->where($search['sql'])->order('wordid desc')->newpage();
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->assign('search', $search['arr']);
        $this->display('newsword/index');
    }

    // 批量删除
    public function batchremove() {
        $optype = request::post('optype', '');
        $ids = request::post('ids', []);
        if ($optype == "del") {
            for ($i = 0; $i < count($ids); $i++) {
                $id = $ids[$i];
                db('newsword')->delete(['wordid' => $id]);
            }
            showmsg('删除成功', '/admin/newsword');
        } else {
            if (is_array($_GET['oldword'])) {
                foreach ($_GET['oldword'] as $id => $val) {
                    $data = array();
                    $data['oldword'] = $_GET['oldword'][$id];
                    $data['newword'] = $_GET['newword'][$id];
                    db('newsword')->where(['wordid' => $id])->update($data);
                }
            }
            if (is_array($_GET['newoldword'])) {
                foreach ($_GET['newoldword'] as $key => $value) {
                    if ($value) {
                        $data = array();
                        $data['oldword'] = $value;
                        $data['newword'] = $_GET['newnewword'][$key];
                        db('newsword')->where(['wordid' => $id])->insert($data);
                    }
                }
            }
            showmsg('更新成功', '/admin/newsword');
        }
    }

    //查询条件
    private function search_frm() {
        $search = request::get('search', []);
        $where = '1=1 ';
        if ($wordid = trim($search['wordid'])) {
            $where .= "and wordid = '{$wordid}' ";
        }
        if ($oldword = trim($search['oldword'])) {
            $where .= "and oldword = '{$oldword}' ";
        }
        if ($newword = trim($search['newword'])) {
            $where .= "and newword = '{$newword}' ";
        }
        return [
            'arr' => $search,
            'sql' => $where
        ];
    }

}
